/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/9/4
 * Time: 22:22
 */
public class Solution {
    public String convert(String string, int numRows) {
        if (numRows == 1) return string;
        //计算公差
        int d = 2 * (numRows - 1),len =  string.length();
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            if (i == 0) {
                int n = i;
                while (n < len){
                    result.append(string.charAt(n));
                    n += d;
                }
            }else if (i == numRows - 1) {
                int n = i;
                while (n < len){
                    result.append(string.charAt(n));
                    n += d;
                }
            }else {
                int n = i;
                int x = d - n;
                while (n < len){
                    result.append(string.charAt(n));
                    if (x < len) {
                        result.append(string.charAt(x));
                    }
                    n += d;
                    x += d;
                }
            }
        }
        return result.toString();
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        String ret = solution.convert("ABC", 3);
        System.out.println(ret);
    }
}